package com.qf.service;

import com.qf.dto.UploadDTO;
import com.qf.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * //                       .::::.
 * //                     .::::::::.
 * //                    :::::::::::  come on
 * //                 ..:::::::::::'
 * //              '::::::::::::'
 * //                .::::::::::
 * //           '::::::::::::::..
 * //                ..::::::::::::.
 * //              ``::::::::::::::::
 * //               ::::``:::::::::'        .:::.
 * //              ::::'   ':::::'       .::::::::.
 * //            .::::'      ::::     .:::::::'::::.
 * //           .:::'       :::::  .:::::::::' ':::::.
 * //          .::'        :::::.:::::::::'      ':::::.
 * //         .::'         ::::::::::::::'         ``::::.
 * //     ...:::           ::::::::::::'              ``::.
 * //    ```` ':.          ':::::::::'                  ::::..
 * //                       '.:::::'                    ':'````..
 * //
 *
 * @author:小川
 * @date: 2018/11/24 11:31
 */
@Service
public class UpLoadServiceImpl implements UpLoadService {
    @Value("${pic_url}")
    private String picurl;
    @Override
    public UploadDTO upload(MultipartFile multipartFile) throws Exception {
        UploadDTO dto=new UploadDTO();
        if(multipartFile==null){
            dto.setError(-1);
            dto.setMeg("上传失败");
            return dto;
        }else {
            String filename = multipartFile.getOriginalFilename();
            String substring = filename.substring(filename.lastIndexOf(".") + 1);

            FastDFSClient dfsClient=new FastDFSClient("properties/client.conf");
            String[] strings = dfsClient.uploadFile(multipartFile.getBytes(), substring);
            String url=picurl+strings[0]+"/"+strings[1];
            dto.setUrl(url);
            dto.setError(0);
            dto.setMeg("上传成功");
            return dto;
        }
    }
}
